package com.petlab.forum.source;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.petlab.common.FilePath;
import com.petlab.common.GetCurrentTime;
import com.petlab.model.Article;
import com.petlab.model.Comment;
import com.petlab.service.ArticleService;

@Component("addCommentContent")
public class AddCommentContent implements AddObjectContent {

	private ArticleService articleService;

	public ArticleService getArticleService() {
		return articleService;
	}

	@Resource
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public void addContent(Object o) {
		// TODO Auto-generated method stub

		Comment comment = (Comment) o;

		String time = GetCurrentTime.getTime();

		int articleId = comment.getArticleId();
		Article article = articleService.getArticleById(articleId);
		String themeTitle = article.getThemeTitle();

		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("authorName", comment.getAuthorName());
		hashMap.put("comContent", comment.getComContent());
		hashMap.put("inTime", time);

		String filename = FilePath
				.getParentDirectory(FilePath.getParentDirectory(this.getClass()
						.getResource("/").getFile()))
				+ "forum/" + themeTitle + "/" + article.getArticleId() + ".xml";

		CreatCommentFile.creatFile(filename, hashMap);

	}

}
